package main.java.com.library.dao.impl;

import com.library.bean.User;
import com.library.dao.BaseDao;
import com.library.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    //添加用户的方法
    @Override
    public int insertUser(User user) {
        int row = 0;
        Connection con = null;
        PreparedStatement pstm = null;
        String sql = "INSERT INTO user (username, password, realName) VALUES (?,?,?)";

        try {
            con = BaseDao.getConnection();
            pstm = con.prepareStatement(sql);
            pstm.setString(1, user.getUsername());
            pstm.setString(2, user.getPassword());
            pstm.setString(3, user.getRealName());
            row = pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(con,pstm,null);
        }

        return row;
    }

    @Override
    public User selectUserByUserName(String userName) {
        User user = null;
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet res = null;
        String sql = "SELECT * FROM user WHERE username = ?";
        try {
            con = BaseDao.getConnection();
            pstm = con.prepareStatement(sql);
            pstm.setString(1, userName);
            res = pstm.executeQuery();  pstm = con.prepareStatement(sql);
            pstm.setString(1, userName);
            res = pstm.executeQuery();
            if(res.next()){
                user = new User();
                user.setUserId(res.getInt("userId"));
                user.setUsername(res.getString("username"));
                user.setPassword(res.getString("password"));
                user.setRealName(res.getString("realName"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(con,pstm,res);
        }

        return user;
    }

    @Override
    public User selectUserByUserNameAndPassword(String userName, String password) {
        User user = null;
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet res = null;
        String sql = "SELECT * FROM user WHERE username =? AND password =?";
        try {
            con = BaseDao.getConnection();
            pstm = con.prepareStatement(sql);
            pstm.setString(1, userName);
            pstm.setString(2, password);
            res = pstm.executeQuery();
            if(res.next()){
                user = new User();
                user.setUserId(res.getInt("userId"));
                user.setUsername(res.getString("username"));
                user.setPassword(res.getString("password"));
                user.setRealName(res.getString("realName"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(con,pstm,res);
        }

        return user;
    }
}
